package com.kai.steering.data.service.ctl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.kai.steering.data.service.entity.domainObject.KnowledgeDO;
import com.kai.steering.data.service.entity.po.Knowledge;
import com.kai.steering.data.service.entity.vo.KnowledgeVO;
import com.kai.steering.data.service.service.KnowledgeService;
import com.kai.steering.data.service.entity.dto.KnowledgeQueryDTO;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.ctl.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import javax.validation.Valid;

import com.kai.steering.common.vo.Page;
import com.kai.steering.common.web.vo.ReturnCodeExt;

import java.util.Date;
import java.util.List;


/**
 * <pre>
 * 知识点 前端控制器
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@RestController
@RequestMapping("/knowledge")
@Api(tags = "知识点 API")
public class KnowledgeController extends BaseController {
    @Autowired
    private KnowledgeService knowledgeService;


    @PostMapping
    @ApiOperation(value = "知识点-添加")
    public Result<Boolean> addKnowledge(@Valid @RequestBody KnowledgeQueryDTO knowledgeQueryDTO) {
        /**
         * 参数检查
         */

        if (StrUtil.isEmpty(knowledgeQueryDTO.getKnowledgeName())) {
           return Result.failed("名字为空");
        }
        /**
         * 构建对象
         */
        KnowledgeDO knowledgeDO = new KnowledgeDO(knowledgeQueryDTO);
        knowledgeDO.setKnowledgeId(null);
        /**
         * 保存对象
         */
        boolean flag = knowledgeService.saveKnowledge(knowledgeDO);
        if (flag){
            return Result.ok();
        }

        return Result.failed();
    }


    @PutMapping("/{id}")
    @ApiOperation(value = "知识点-编辑")
    public Result<Boolean> updateKnowledge(@PathVariable("id") Serializable id, @Valid @RequestBody KnowledgeQueryDTO knowledgeQueryDTO) {
        /**
         * 参数检查
         */
        if (StrUtil.isEmpty(knowledgeQueryDTO.getKnowledgeName())) {
            return Result.failed("名字为空");
        }
        if (StrUtil.isEmpty(id.toString())) {
            return Result.failed("id为空");
        }
        /**
         * 构建对象
         */
        KnowledgeDO knowledgeDO = new KnowledgeDO(knowledgeQueryDTO);

        //修改属性
        boolean flag = knowledgeService.updateKnowledge(id, knowledgeDO);
        if (flag){
            return Result.ok();
        }
        return Result.failed();
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "知识点-删除")
    public Result<Boolean> deleteKnowledge(@PathVariable("id") String id) {
        /**
         * 参数验证
         */
        //id
        if (StringUtils.isEmpty(id.toString())) {
            return Result.failed("id为空");
        }
        boolean  flag = knowledgeService.deleteKnowledge(id);
        if (flag) {
            return Result.ok();
        }

        return Result.failed();
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "知识点-详情")
    public Result<KnowledgeVO> getKnowledge(@PathVariable("id") String id) {
        /**
         * 参数验证
         */
        if (StringUtils.isEmpty(id)) {
            return Result.failed("id为空");
        }
        /**
         * 服务
         */
        KnowledgeDO knowledgeDO = knowledgeService.getKnowledgeById(id);


        KnowledgeVO knowledgeQueryVo = new KnowledgeVO(knowledgeDO);
        if (knowledgeQueryVo == null) {
            return Result.ok(knowledgeQueryVo);
        }

        return Result.failed();
    }


    @PostMapping("/queryKnowledge")
    @ApiOperation(value = "知识点-综合查询")
    public Result<Page<KnowledgeVO>> getKnowledgePageList(@Valid @RequestBody KnowledgeQueryDTO knowledgeQueryDTO) {
        Page<KnowledgeVO> paging = knowledgeService.getKnowledgePageList(knowledgeQueryDTO);
        return Result.ok(paging);
    }


//    @GetMapping("/list")
//    @ApiOperation(value = "知识点-全部列表")
//    public Result<List<KnowledgeVO>> getKnowledgeAllList() {
//
//        List<KnowledgeVO> list = null;
//        return Result.ok(list);
//    }

    @GetMapping("/treeKnowledge/{id}")
    @ApiOperation(value = "知识点-知识点树")
    public Result<KnowledgeVO> treeKnowledge(@PathVariable("id") String id) {
        /**
         * 参数验证
         */
        if (StringUtils.isEmpty(id)) {
            return Result.failed("id为空");
        }

        /**
         * 调用服务
         */
        KnowledgeDO knowledgeDO= knowledgeService.treeKnowledge(id);
        KnowledgeVO knowledgeVO = new KnowledgeVO(knowledgeDO);
        return Result.ok(knowledgeVO);
    }
}